#include <conio.h>
#include <stdio.h>
#include <windows.h>
#include "../../HeadFiles/MD5.h"
#include "../../HeadFiles/StandardHeads.h"
#include "../../HeadFiles/UsedStd.h"
#include "../../HeadFiles/PagesH/LoginPage.h"
/*根据输入做出反馈的welcome函数*/
// administor 返回1时登录成功，返回0时登录失败，返回-1时文件丢失
// user 返回1时登录成功，返回0时登录失败，返回-1时文件丢失
// sign up 返回1，需要重新登录（用户已存在） 返回0，注册成功 返回2，需要重新注册（两次密码不一致）
/*
  welcome函数的详细说明
  choice=1 用户登录
 $登陆成功 清屏 返回1
  登陆失败 清屏 输出错误原因 让choice=0 重新显示开始界面并选择
  登录异常（找不到文件，原因是还没有用户注册，提示让他去注册）清屏 输出错误原因 让choice=0 重新显示开始界面并选择
  choice=2 管理员登录
 $登陆成功 清屏 返回2
  登陆失败  清屏 输出错误原因 让choice=0 重新显示开始界面并选择
  登录异常（找不到文件，原因是管理员的密码本异常）清屏 输出错误原因 让choice=0 重新显示开始界面并选择
  choice=3 用户注册
  注册失败 清屏 输出注册失败提示字样（账号已被注册）（提示其登录） 让choice=0 重新显示开始界面并选择
  注册成功 清屏 输出注册成功提示字样（提示其登录）让choice=0 重新显示开始界面并选择
  注册失败 清屏 输出注册失败提示字样（两次输入密码不一致）（提示其重试） 让choice=0 重新显示开始界面并选择
  choice=4 管理员或用户退出
 $清屏 输出谢谢使用字样 返回0
  choice=5 无效的输入 清屏 输出提示字样 让choice=0 重新显示开始界面并选择
*/
char userNameCpy[60];
int wrongPwdTimes = 0;
void add_uset_to_client(void)
{
    struct ClientNode * now = (struct ClientNode *) malloc(sizeof(struct ClientNode));
    now->preClient = now->nextClient = NULL;    // 申请新节点第一件事就是指针初始化！非常重要！
    now->balance = 0.00;
    strcpy(now->clientName,userNameCpy);
    now->lv=0;
    getCurrentTime(&now->year, &now->month, &now->day);
    clientTail->nextClient = now;
    now->preClient =  clientTail;
    clientTail = now;
    rebuildClientFile(clientHead);
}
void LoginPut(void)
{
    system("cls");
    puts("Please log in or sign up:");
    printf("type 1 to log in user\n");
    printf("type 2 to log in administrator\n");
    printf("type 3 to sign up\n");
    printf("type 0 to exit\n");
    puts(SPLIT_STR);
}

int LoginPage(char yourName[])
//传入用户本和管理员本
// 根据用户的输入来进行值的反馈
{
    int i = 0, j = 0, k = 0;
    bool isComputerSecurity = true;
    FILE *fileLockedUser;
    fileLockedUser = fopen("../Data/LoginData/LockedUser.txt", "r");
    char tempComputerName[MAX_NAME];
    char computerName[MAX_NAME];
    getComputerName(computerName);//判断此电脑用户名是否被锁住了
    while(fscanf(fileLockedUser, "%s\n", tempComputerName) != EOF)
    {
        if (strcmp(computerName, tempComputerName) == 0)
        {
            isComputerSecurity = false;
            printf("Your computer Your computer is locked, contact administrator to unblock it\n");
            system("pause");//暂停,显现提醒语句
            fclose(fileLockedUser);
            exit(0);//直接退出程序
        }
    }
    fclose(fileLockedUser);
    FILE *fileWrongPwdTimes;
    fileWrongPwdTimes = fopen("../Data/LoginData/WrongPwdTimes.txt", "r");
    fscanf(fileWrongPwdTimes, "%d\n", &wrongPwdTimes);
    while (isComputerSecurity)
    {
        LoginPut();
        char opt0 = getche();
        while(!(opt0 == '1' || opt0 == '2' || opt0 == '3' || opt0 == '0'))
        {
            printf("\n");
            puts("Illegal input detected! Try again:");
            opt0 = getche();
        }
        switch (opt0)
        {
            case '1':
                i = loginUser(yourName);
                if (i == 1)//$登陆成功 清屏 返回1
                {
                    system("cls");
                    printf("Login succeeded.\n");
                    wrongPwdTimes = 0;//登录成功后清空输入密码错误的次数
                    //此处逻辑是否合适？
                    fileWrongPwdTimes = fopen("../Data/LoginData/WrongPwdTimes.txt", "w");
                    fprintf(fileWrongPwdTimes, "%d\n", wrongPwdTimes);
                    fclose(fileWrongPwdTimes);
                    system("pause");
                    return 1;
                }
                if (i == 0)
                    // 登陆失败 清屏 输出错误原因 让choice='0' 重新显示开始界面并选择
                {
                    system("cls");
                    printf("Wrong username or password.\n");
                    wrongPwdTimes++;//用此变量记录输入密码错误的次数
                    fprintf(fileWrongPwdTimes, "%d\n", wrongPwdTimes);
                    fclose(fileWrongPwdTimes);
                    fileWrongPwdTimes = fopen("../Data/LoginData/WrongPwdTimes.txt", "w");
                    if(!(wrongPwdTimes == 5))
                        printf("You only have %d chances to enter now.\n", (5 - wrongPwdTimes));//输出剩余次数
                    else
                    {
                        printf("Your computer Your computer is locked, contact administrator to unblock it.\n");
                        fileLockedUser = fopen("../Data/LoginData/LockedUser.txt", "a+");
                        fprintf(fileLockedUser, "%s\n", computerName);
                        system("pause");//暂停,显现提醒语句
                        exit(0);
                    }
                    system("pause");//暂停,显现提醒语句
                    break;
                }
                if (i == -1)
                    //登录异常（找不到文件，原因是还没有用户注册，提示让他去注册）清屏 输出错误原因 让choice='0' 重新显示开始界面并选择
                {
                    system("cls");
                    printf("Unable to open file,maybe you need to register first.\n");
                  
                    system("pause");//暂停,显现提醒语句
                    break;
                }
            case '2':
                j = loginAdmin();
                if (j == 1)
                    //$登陆成功 清屏 返回2
                {
                    system("cls");
                    printf("Login succeeded.\n");
                    wrongPwdTimes = 0;//登录成功后清空输入密码错误的次数
                    //此处逻辑是否合适？
                    fileWrongPwdTimes = fopen("../Data/LoginData/WrongPwdTimes.txt", "w");
                    fprintf(fileWrongPwdTimes, "%d\n", wrongPwdTimes);
                    fclose(fileWrongPwdTimes);
                    system("pause");
                    return 2;
                }
                if (j == 0)
                    //登陆失败  清屏 输出错误原因 让choice=0 重新显示开始界面并选择
                {
                    system("cls");
                    printf("Wrong username or password.\n");
                    wrongPwdTimes++;//用此变量记录输入密码错误的次数
                    fileWrongPwdTimes = fopen("../Data/LoginData/WrongPwdTimes.txt", "w");
                    fprintf(fileWrongPwdTimes, "%d\n", wrongPwdTimes);
                    fclose(fileWrongPwdTimes);
                    if(!(wrongPwdTimes == 5))
                        printf("You only have %d chances to enter now.\n", (5 - wrongPwdTimes));//输出剩余次数
                    else
                    {
                        printf("Your computer Your computer is locked, contact administrator to unblock it.\n");
                        fileLockedUser = fopen("../Data/LoginData/LockedUser.txt", "a+");
                        fprintf(fileLockedUser, "%s\n", computerName);
                        system("pause");//暂停,显现提醒语句
                        exit(0);
                    }
                    system("pause");//暂停,显现提醒语句
                    break;
                }
                if (j == -1)
                    //登录异常（找不到文件，原因是管理员的密码本异常）清屏 输出错误原因 让choice=0 重新显示开始界面并选择
                {
                    system("cls");
                    printf("Unable to open file,please check the administrator data.\n");
                    
                    system("pause");//暂停,显现提醒语句
                    break;
                }
            case '3':
                k = registerUser();
                if (k == 1)
                    //注册失败 清屏 输出注册失败提示字样（账号已被注册）（提示其登录） 让choice=0 重新显示开始界面并选择
                {
                    system("cls");
                    printf("This account already exists, please try to log in. \n");
                    system("pause");//暂停,显现提醒语句
                    break;
                }
                if (k == 0)
                    //注册成功 清屏 输出注册成功提示字样（提示其登录）让choice=0 重新显示开始界面并选择
                {
                    system("cls");
                    printf("Registered successfully.Please try to log in.\n");
                    system("pause");//暂停,显现提醒语句
                    add_uset_to_client();
                    break;
                }
                if (k == 2)
                    //注册失败 清屏 输出注册失败提示字样（两次输入密码不一致）（提示其重试） 让choice=0 重新显示开始界面并选择
                {
                    system("cls");
                    printf("the password is inconsistent, please again.\n");
                  
                    system("pause");//暂停,显现提醒语句
                    break;
                }
            case '0':
                //$清屏 输出谢谢使用字样 返回0
                system("cls");
                printf("Thanks for using.\n");
                system("pause");//暂停,显现提醒语句
                return 0;
            default:
                //无效的输入 清屏 输出提示字样 让choice=0 重新显示开始界面并选择
                system("cls");
                printf("Invalid option, please re-enter!\n");
                system("pause");
                // 无效的选项 重新选择
                break;
        }
    }
}